package com.hdwe.tools.pm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hdwe.tools.pm.domain.bo.PmTaskStepBo;
import com.hdwe.tools.pm.domain.vo.PmTaskStepVo;
import com.hdwe.tools.pm.service.IPmTaskStepService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务步骤
 *
 * @author Lion Li
 * @date 2024-12-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/taskStep")
public class PmTaskStepController extends BaseController {

    private final IPmTaskStepService pmTaskStepService;

    /**
     * 查询任务步骤列表
     */
    @SaCheckPermission("pm:taskStep:list")
    @GetMapping("/list")
    public TableDataInfo<PmTaskStepVo> list(PmTaskStepBo bo, PageQuery pageQuery) {
        return pmTaskStepService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务步骤列表
     */
    @SaCheckPermission("pm:taskStep:export")
    @Log(title = "任务步骤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmTaskStepBo bo, HttpServletResponse response) {
        List<PmTaskStepVo> list = pmTaskStepService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务步骤", PmTaskStepVo.class, response);
    }

    /**
     * 获取任务步骤详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pm:taskStep:query")
    @GetMapping("/{id}")
    public R<PmTaskStepVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(pmTaskStepService.queryById(id));
    }

    /**
     * 新增任务步骤
     */
    @SaCheckPermission("pm:taskStep:add")
    @Log(title = "任务步骤", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTaskStepBo bo) {
        return toAjax(pmTaskStepService.insertByBo(bo));
    }

    /**
     * 修改任务步骤
     */
    @SaCheckPermission("pm:taskStep:edit")
    @Log(title = "任务步骤", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTaskStepBo bo) {
        return toAjax(pmTaskStepService.updateByBo(bo));
    }

    /**
     * 删除任务步骤
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pm:taskStep:remove")
    @Log(title = "任务步骤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pmTaskStepService.deleteWithValidByIds(List.of(ids), true));
    }
}
